package com.oa.admin.controller;

import com.joneying.common.config.SecurityConfig;
import com.joneying.common.web.response.Resp;
import com.oa.admin.dto.CertificationAddDTO;
import com.oa.admin.dto.CertificationListDTO;
import com.oa.admin.entity.Certification;
import com.oa.admin.service.ICertificationService;
import com.oa.admin.vo.CertificationDetailsVO;
import com.oa.admin.vo.CertificationListVO;
import com.oa.core.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.joneying.common.annotation.CommonController;
import org.springframework.context.annotation.Description;

import java.util.List;

import org.springframework.web.bind.annotation.RestController;
import com.oa.core.BaseController;

/**
 * @author syb123
 * @since 2019-01-22
 */
@RestController
@RequestMapping("/certification")
@Api(description = "认证管理模块")
public class CertificationController extends BaseController {

    private final ICertificationService certificationService;

    @Autowired
    public CertificationController(ICertificationService certificationService) {
        this.certificationService = certificationService;
    }

    /**
     * 查询列表
     *
     * @return Resp
     */
    @GetMapping("/list")
    @CommonController
    @Description("查询所有")
    @ApiOperation(value = "查询（不传page则查询所有）")
    public Resp<PageResult<CertificationListVO>> findList(CertificationListDTO certificationListDTO) {
        return new Resp().success(certificationService.findCertificationList(certificationListDTO));
    }

    @GetMapping("/export")
    @CommonController
    @Description("导出")
    @ApiOperation(value = "导出")
    public void export(CertificationListDTO certificationListDTO) {
        certificationService.export(certificationListDTO, response);
    }

    /**
     * 根据Id查询
     *
     * @param id id
     * @return Resp
     */
    @GetMapping("/{id}")
    @CommonController
    @Description("获取详细信息")
    @ApiOperation(value = "根据id查询")
    public Resp<CertificationDetailsVO> findById(@ApiParam(value = "id", required = true) @PathVariable String id) {
        return new Resp().success(certificationService.findCertificationDetails(id));
    }


    /**
     * 新增
     *
     * @param certification Certification
     * @return Resp
     */
    @PostMapping("")
    @CommonController
    @Description("新增")
    @ApiOperation(value = "新增")
    public Resp add(@ApiParam(value = "认证管理对象") @RequestBody(required = false)CertificationAddDTO certification) {
        return certificationService.add(certification);
    }

    /**
     * 根据Id修改
     *
     * @return Resp
     */
    @PutMapping("")
    @CommonController
    @Description("根据id修改")
    @ApiOperation(value = "根据id修改")
    public Resp update(@ApiParam(value = "认证管理对象") @RequestBody(required = false) CertificationAddDTO certificationAddDTO) {
        return certificationService.edit(certificationAddDTO);
    }

    /**
     * 根据Id删除
     *
     * @param id id
     * @return Resp
     */
    @DeleteMapping("/{id}")
    @CommonController
    @Description("根据id删除")
    @ApiOperation(value = "根据id删除")
    public Resp delete(@ApiParam(value = "id", required = true) @PathVariable("id") String id) {
        Certification certification = certificationService.selectById(id);
        if (certification != null && certification.getIsDeleted() == 2) {
            certification.setIsDeleted(1);
            if (certificationService.updateById(certification)) {
                return new Resp().success();
            }
            return new Resp().failure();
        }
        return new Resp().failure();
    }
}
